import 'dart:convert' show json;
import 'package:flutter/cupertino.dart';
import 'package:freedom/const/game_const.dart';
import 'package:freedom/entity/user/verification_mode.dart';
import 'package:freedom/util/cache_util.dart';
import 'package:freedom/util/user_util.dart';

import 'package:freedom/util/data_type.dart';

class UserEntityInfo with ChangeNotifier {
  UserEntityInfo(
      {required this.sysUser,
      this.grantedAuthorities,
      this.authoritiesList,
      this.tokenAuthority,
      this.verificationMode});
  updateUserInfo({String? imagePath}) {
    sysUser.portraitUrl = imagePath;
    UserEntityInfo? userEntityInfo = UserUtil.getUserInfo();
    if (userEntityInfo == null) {
      return;
    }
    userEntityInfo.sysUser.portraitUrl = imagePath;
    CacheUtil.preferencesInstance!
        .setString(CacheConst.userInfo, json.encode(userEntityInfo.toJson()));
    notifyListeners();
  }

  factory UserEntityInfo.fromJson(Map<String, dynamic>? jsonRes) {
    if (jsonRes == null) {
      return UserEntityInfo(sysUser: SysUser.init("", ""));
    }
    final List<GrantedAuthorities>? grantedAuthorities =
        jsonRes['grantedAuthorities'] is List ? <GrantedAuthorities>[] : null;
    if (grantedAuthorities != null) {
      for (final dynamic item in jsonRes['grantedAuthorities']) {
        if (item != null) {
          grantedAuthorities.add(
              GrantedAuthorities.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }

    final List<String>? authoritiesList =
        jsonRes['authoritiesList'] is List ? <String>[] : null;
    if (authoritiesList != null) {
      for (final dynamic item in jsonRes['authoritiesList']) {
        if (item != null && asT<String>(item) != null) {
          authoritiesList.add(asT<String>(item)!);
        }
      }
    }

    return UserEntityInfo(
        sysUser:
            SysUser.fromJson(asT<Map<String, dynamic>>(jsonRes['sysUser'])),
        grantedAuthorities: grantedAuthorities,
        authoritiesList: authoritiesList,
        tokenAuthority: asT<String>(jsonRes['tokenAuthority']),
        verificationMode: asT<VerificationMode>(jsonRes['verificationMode']));
  }

  SysUser sysUser;
  List<GrantedAuthorities>? grantedAuthorities;
  List<String>? authoritiesList;
  String? tokenAuthority;
  VerificationMode? verificationMode;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'sysUser': sysUser,
        'grantedAuthorities': grantedAuthorities,
        'authoritiesList': authoritiesList,
        'tokenAuthority': tokenAuthority,
        'verificationMode': verificationMode
      };

  @override
  String toString() {
    return json.encode(this);
  }

  void updateInfo(UserEntityInfo userInfo) {
    authoritiesList = userInfo.authoritiesList;
    sysUser = userInfo.sysUser;
    tokenAuthority = userInfo.tokenAuthority;
    grantedAuthorities = userInfo.grantedAuthorities;
    verificationMode = userInfo.verificationMode;
  }
}

class SysUser {
  SysUser({
    this.id = 0,
    this.name,
    this.code,
    this.age,
    this.phone,
    this.username,
    this.password,
    this.email,
    this.portraitUrl,
    this.version,
    this.createDate,
    this.updateDate,
    this.updater,
    this.creater,
  });
  SysUser.init(this.username, this.password, {this.id = 0});

  factory SysUser.fromJson(Map<String, dynamic>? jsonRes) {
    if (jsonRes == null) {
      return SysUser();
    }
    return SysUser(
      id: asT<int>(jsonRes['id']) ?? 0,
      name: asT<String>(jsonRes['name']),
      code: asT<String>(jsonRes['code']),
      age: asT<int>(jsonRes['age']),
      phone: asT<String>(jsonRes['phone']),
      username: asT<String>(jsonRes['username']),
      password: asT<String>(jsonRes['password']),
      email: asT<String>(jsonRes['email']),
      portraitUrl: asT<String>(jsonRes['portraitUrl']),
      version: asT<String>(jsonRes['version']),
      createDate: asT<String>(jsonRes['createDate']),
      updateDate: asT<String>(jsonRes['updateDate']),
      updater: asT<String>(jsonRes['updater']),
      creater: asT<String>(jsonRes['creater']),
    );
  }

  int id;
  String? name;
  String? code;
  int? age;
  String? phone;
  String? username;
  String? password;
  String? email;
  String? portraitUrl;
  String? version;
  String? createDate;
  String? updateDate;
  String? updater;
  String? creater;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'name': name,
        'code': code,
        'age': age,
        'phone': phone,
        'username': username,
        'password': password,
        'email': email,
        'portraitUrl': portraitUrl,
        'version': version,
        'createDate': createDate,
        'updateDate': updateDate,
        'updater': updater,
        'creater': creater,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class GrantedAuthorities {
  GrantedAuthorities({
    this.authority,
  });

  factory GrantedAuthorities.fromJson(Map<String, dynamic>? jsonRes) {
    if (jsonRes == null) {
      return GrantedAuthorities();
    }
    return GrantedAuthorities(
      authority: asT<String>(jsonRes['authority']),
    );
  }

  String? authority;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'authority': authority,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
